import 'package:honeypotflutter/modules/account/model/account_withdraw_history_model.dart';
import 'package:honeypotflutter/net/public_http.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_modal.dart';

class AccountApi {
  /*
  *  提现记录
  * */
  static Future<List<AccountWithdrawHistoryItemModel>> withdrawHistory(String StartTime, String EndTime, int page) async {
    try {
      Map<String, dynamic> param = {
        "StartTime": StartTime,
        "EndTime": EndTime,
        "page": page,
        "pageSize": 10,
      };
      Map<String, dynamic> result = await MGHttp.get(
        "api/services/app/Account/GetMyWalletWithdrawPageList",
        params: param,
      );
      AccountWithdrawHistoryModel model = AccountWithdrawHistoryModel.fromJson(result);
      return model.items ?? [];
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 转账
  * */
  static Future<bool> transferBalance({required String amount, required int type, String? aliPayAccount}) async {
    try {
      Map<String, dynamic> param = {
        "amount": amount,
        "type": type,
        "aliPayAccount": aliPayAccount,
      };
      String result = await MGHttp.post(
        "api/services/app/Account/CreateWalletWithdraw",
        params: param,
      );
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  *  签约
  * */
  static Future<bool> signInfo({String? accountName, String? idCardNumber, String? bankCardNumber, String? reservedPhoneNumber}) async {
    try {
      Map<String, dynamic> param = {
        "accountName": accountName,
        "idCardNumber": idCardNumber,
        "bankCardNumber": bankCardNumber,
        "reservedPhoneNumber": reservedPhoneNumber,
      };
      String result = await MGHttp.post("api/services/app/Account/CreateWalletWithdraw", params: param);
      await _bindBank(result);
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 绑定银行
  * */
  static Future<bool> _bindBank(String accountId) async {
    try {
      Map<String, dynamic> param = {"accountId": accountId};
      String result = await MGHttp.post("api/services/app/BankAccount/BindBankAccount", params: param);
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }
}
